package September10;

import java.io.File;

public class DirectorySize {
    public static void main(String[] args) {
        // 替换为你想要计算大小的目录路径
        String directoryPath = "D:\\IdeaProjects\\untitled8\\src\\September6";

        // 创建File对象
        File directory = new File(directoryPath);

        // 调用方法计算目录大小
        long size = directorySize(directory);

        // 输出目录大小
        System.out.println("The size of the directory is: " + size + " bytes");
    }

    public static long directorySize(File directory) {
        long size = 0;
        // 获取文件列表
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                // 如果是目录，则递归计算其大小
                if (file.isDirectory()) {
                    size += directorySize(file);
                } else {
                    // 累加文件大小
                    size += file.length();
                }
            }
        }
        return size;
    }
}
